import { Vue } from 'vue-property-decorator';
import moment from 'moment';
import { PrefixInteger, numberFormat, unitShow, dateFormat, objToVxeFilterArr, getConfigItem, timeFormat, uniqueFunc } from '@/utils/tools';
import DictionConstUtil from '@/utils/diction-const-util';
import { ColumnVO } from '@/common/vo';

//
export default class ETFColumnUtil extends Vue {
    SHOW_SECURITY = getConfigItem('SHOW_SECURITY');

    // 券商模式下的额外字段展示
    otherAssetColumns = [
        { content: '资产单元编号', id: 'assetCode', width: 140, minWidth: 140 },
        { content: '资产单元名称', id: 'assetName', width: 140, minWidth: 140 },
    ];

    // 指令状态
    instructionStatus = {
        '0': '已归档',
        '1': '已下达成功',
        '3': '已撤销',
    };
    /**
     * 替代标志
     */
    cashTypePojo = {
        '0': '禁止',
        '1': '允许',
        '2': '必须',
    };
    /**
     * 交易市场
     */
    marketPojo = {
        '1': '深交所',
        '2': '上交所',
    };
    /**
     * ETF类型
     */
    ETFTypePojo = {
        '1': '股票ETF',
        '2': '债券ETF',
        '3': '交易型货币基金',
        '4': '黄金ETF',
        '5': '货币市场基金',
        '6': '沪港通ETF',
        '7': '商品期货ETF',
    };
    /**
     * ETF市场类型
     */
    ETFMarketTypePojo = {
        '1': '本市场ETF',
        '2': '跨境ETF',
        '3': '跨市场ETF(交易所)',
        '4': '跨市场ETF(中登)',
        '5': '沪港通ETF',
        '6': '深港通ETF',
    };

    /**
     * 接口类型
     */
    importTypePojo = {
        '1': '公告1.0接口',
        '2': '交易2.0接口',
        '3': '中登接口',
        '4': '5.0接口',
        '5': '公告2.1接口',
        '6': '交易2.1接口',
        '7': '银行间XML接口',
    };
    yesOrNoPojo = {
        '0': '否',
        '1': '是',
    };

    creationredemTypePojo = {
        '0': '不允许进行申购和赎回',
        '1': '允许申购和赎回',
        '2': '允许申购不允许赎回',
        '3': '不允许申购允许赎回',
    };

    clearSpeedPojo = {
        '0': 'T+0',
        '1': 'T+1',
    };

    /** ******************************************* ETF指令start ******************************************** */

    formatterNumber(number: any) {
        return number != undefined ? numberFormat(number, 2) : '--';
    }

    // 表格列筛选数据
    filterHandler({ option, row, column }) {
        const property = column['property'];
        return row[property].includes(option.data);
    }

    /**
     * 成分股信息表格字段
     */
    constituentStockColumns: any[] = [
        // { field: 'serial', title: '序号', sortable: true, minWidth: 100, filterType: 'search', filters: [{ data: '' }] },
        {
            field: 'marketCode',
            title: '交易所',
            sortable: true,
            minWidth: 140,
            filters: objToVxeFilterArr(this.marketPojo),
            align: 'center',
            render: value => this.marketPojo[value] || value,
        },
        {
            field: 'stockCode',
            title: '证券代码',
            sortable: true,
            minWidth: 140,
            filterType: 'search',
            filters: [{}],
            filterMethod: this.filterHandler,
        },
        {
            field: 'stockName',
            title: '证券名称',
            sortable: true,
            minWidth: 140,
            filterType: 'search',
            filters: [{}],
            filterMethod: this.filterHandler,
        },
        {
            field: 'positionAmount',
            title: '持仓数量',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'availableAmount',
            title: '可用数量',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'stockAmount',
            title: '单位数量',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        { field: 'lastPrice', title: '价格', sortable: true, minWidth: 120, align: 'right', render: (value, row) => this.formatterNumber(value) },
        { field: 'marketValue', title: '市值', sortable: true, minWidth: 120, align: 'right', render: (value, row) => this.formatterNumber(value) },
        /** ******* 基金申购显示start ******** */
        {
            field: 'calApplyAmount',
            title: '计算申购数量',
            sortable: true,
            minWidth: 160,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'actualApplyAmount',
            title: '实际申购数量',
            sortable: true,
            minWidth: 160,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'priceRatio',
            title: '溢价比率',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        /** ******* 基金申购显示end ******** */
        /** ******* 基金赎回显示start ******** */
        {
            field: 'calRedeemAmount',
            title: '计算赎回数量',
            sortable: true,
            minWidth: 160,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'actualRedeemAmount',
            title: '实际赎回数量',
            sortable: true,
            minWidth: 160,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'priceRatio',
            title: '折价比率',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        /** ******* 基金赎回显示end ******** */
        {
            field: 'cashType',
            title: '替代标志',
            sortable: true,
            minWidth: 140,
            filters: objToVxeFilterArr(this.cashTypePojo),
            align: 'center',
            render: value => this.cashTypePojo[value] || value,
        },
        {
            field: 'alternativeAmount',
            title: '替代数量',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        },
        {
            field: 'purchaseBalance',
            title: '替代金额',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: (value, row) => this.formatterNumber(value),
        }, // purchaseBalance 申购替代金额，redeemBalance 赎回替代金额
    ];

    /**
     * ETF指令信息表格字段
     */
    ETFOrderInfoColumns: any[] = [
        { content: '指令序号', id: 'instNo', minWidth: 120, fixed: 'left' },
        {
            content: '指令状态',
            id: 'instStatus',
            minWidth: 120,
            fixed: 'right',
            needFormat: true,
            formatter: value => (value != undefined ? this.instructionStatus[value] : '--'),
        },
        { content: '产品代码', id: 'fundCode', minWidth: 120, fixed: 'left' },
        { content: '产品名称', id: 'fundName', minWidth: 120 },
        { content: '组合编码', id: 'groupCode', minWidth: 150 },
        { content: '组合名称', id: 'groupName', minWidth: 150 },
        { content: '证券代码', id: 'stockCode', minWidth: 120 },
        { content: '证券名称', id: 'stockName', minWidth: 120 },
        { content: '证券类别', id: 'stockType', minWidth: 120 },
        { content: '业务类型', id: 'businessTypeName', minWidth: 120 },
        { content: '委托方向', id: 'orderDirectionName', minWidth: 120 },
        { content: '投资类型', id: 'investTypeName', minWidth: 120 },
        { content: '币种', id: 'curName', minWidth: 120 },
        { content: '交易市场', id: 'marketName', minWidth: 120 },
        { content: '指令价格(元)', id: 'orderPrice', minWidth: 140, needFormat: true, formatter: (value, row) => this.formatterNumber(value) },
        { content: '指令数量(篮)', id: 'orderAmount', minWidth: 140, needFormat: true, formatter: (value, row) => this.formatterNumber(value) },
        { content: '指令金额(元)', id: 'orderBalance', minWidth: 140, needFormat: true, formatter: (value, row) => this.formatterNumber(value) },

        {
            content: 'T日预估现金差额(元)',
            id: 'estimatecash',
            minWidth: 200,
            needFormat: true,
            formatter: (value, row) => this.formatterNumber(value),
        },
        {
            content: 'T日预估现金差额汇总(元)',
            id: 'totalEstimatecash',
            minWidth: 220,
            needFormat: true,
            formatter: (value, row) => this.formatterNumber(value),
        },
        {
            content: '现金替代总金额(元)',
            id: 'totalAlternativeAmount',
            minWidth: 200,
            needFormat: true,
            formatter: (value, row) => this.formatterNumber(value),
        },

        { content: '下达人', id: 'directUserName', minWidth: 100 },
        {
            content: '下达时间',
            id: 'directTime',
            minWidth: 120,
            needFormat: true,
            formatter: (value, row) => dateFormat(row['directDate']) + ' ' + timeFormat(row['directTime']) || '--',
        },
        { content: '操作', id: 'operate', minWidth: 80, fixed: 'right' },
    ];

    /**
     * 指令明细字段 （同信息表格字段，但部分字段需要调整显示顺序，并去掉操作列）
     */
    getETFOrderDetailColumns() {
        let columns = [].concat(this.ETFOrderInfoColumns);
        // 指定位置插入下达人、下达时间、指令价格、指令数量、指令金额
        const tempColumns = [columns[20], columns[21], columns[14], columns[15], columns[16]];
        columns.splice(2, 0, ...tempColumns);
        // 去除重复列
        columns = uniqueFunc(columns, 'id');
        if (this.SHOW_SECURITY) {
            // 添加券商模式字段
            columns.splice(9, 0, ...this.otherAssetColumns);
        }
        // 去除操作列
        columns.pop();
        return columns;
    }

    /**
     * ETF特有排序字段
     */
    etfSortFields = {
        estimatecash: 'N_ESTIMATECASH',
        totalEstimatecash: 'N_TOTALESTIMATECASH',
        totalAlternativeAmount: 'N_TOTAL_ALTERNATIVE_AMOUNT',
    };

    /** ******************************************* ETF指令end ******************************************** */

    /** ******************************************* PCF基础数据start ******************************************** */

    /**
     * ETF信息表排序字段
     */
    etfBasicSortFields: any = {
        assetDealDate: 'D_ASSET_DEAL_DATE',
        cash: 'N_CASH',
        cashCode: 'C_CASH_CODE',
        cashCode2: 'C_CASH_CODE2',
        creationLimitPeruser: 'N_CREATION_LIMIT_PERUSER',
        creationredemType: 'C_CREATIONREDEM_TYPE',
        date: 'D_DATE',
        estimatecash: 'N_ESTIMATECASH',
        etfCode: 'C_ETF_CODE',
        etfType: 'C_ETF_TYPE',
        execType: 'C_EXEC_TYPE',
        fileDate: 'D_FILE_DATE',
        impactCostRate: 'N_IMPACT_COST_RATE',
        importType: 'C_IMPORT_TYPE',
        marketCode: 'C_MARKET_CODE',
        marketName: 'C_MARKET_NAME',
        maxcashRatio: 'N_MAXCASH_RATIO',
        nav: 'N_NAV',
        navPercu: 'N_NAV_PERCU',
        preTradeDate: 'D_PRE_TRADE_DATE',
        punblish: 'C_PUNBLISH',
        purchaseClearSpeed2: 'C_PURCHASE_CLEAR_SPEED2',
        recordNum: 'N_RECORD_NUM',
        redeemClearSpeed2: 'C_REDEEM_CLEAR_SPEED2',
        redemptionLimitPeruser: 'N_REDEMPTION_LIMIT_PERUSER',
        reportUnit: 'N_REPORT_UNIT',
        rivalMarket: 'C_RIVAL_MARKET',
        stockCode: 'C_STOCK_CODE',
        stockName: 'C_STOCK_NAME',
        todayCanRedeem: 'C_TODAY_CAN_REDEEM',
        tradeDate: 'D_TRADE_DATE',
        type: 'C_TYPE',
    };

    /**
     * ETF信息表格字段
     */
    ETFTableColumns: ColumnVO[] = [
        {
            title: '日期',
            field: 'date',
            minWidth: 120,
            sortable: true,
            // filterType: 'search',
            // filters: [{}],
            align: 'center',
            render: value => dateFormat(value) || '--',
        },
        {
            title: '交易市场',
            field: 'marketCode',
            minWidth: 120,
            sortable: true,
            filters: objToVxeFilterArr(this.marketPojo),
            align: 'center',
            render: value => this.marketPojo[value] || value,
        },
        { title: '二级市场代码', field: 'stockCode', minWidth: 160, sortable: true, filterType: 'search', filters: [{}], align: 'left' },
        { title: '代码名称', field: 'stockName', minWidth: 120, sortable: true, filterType: 'search', filters: [{}], align: 'left' },
        { title: '一级市场代码', field: 'etfCode', minWidth: 160, sortable: true, filterType: 'search', filters: [{}], align: 'left' },
        { title: '资金代码', field: 'cashCode', minWidth: 120, sortable: true, filterType: 'search', filters: [{}], align: 'left' },
        { title: '资金代码2', field: 'cashCode2', minWidth: 140, sortable: true, filterType: 'search', filters: [{}], align: 'left' },
        {
            title: '资金代码2申购清算速度',
            field: 'purchaseClearSpeed2',
            minWidth: 220,
            sortable: true,
            filters: [
                { label: 'T+0', value: '0' },
                { label: 'T+1', value: '1' },
            ],
            align: 'center',
            render: value => this.clearSpeedPojo[value] || '--',
        },
        {
            title: '资金代码2赎回清算速度',
            field: 'redeemClearSpeed2',
            minWidth: 220,
            sortable: true,
            filters: [
                { label: 'T+0', value: '0' },
                { label: 'T+1', value: '1' },
            ],
            align: 'center',
            render: value => this.clearSpeedPojo[value] || '--',
        },
        { title: '申报单位', field: 'reportUnit', minWidth: 120, sortable: true, align: 'right', render: value => this.formatterNumber(value) },
        {
            title: '现金比例上限',
            field: 'maxcashRatio',
            minWidth: 150,
            sortable: true,
            align: 'right',
            render: value => this.formatterNumber(value),
        },
        {
            title: '是否发布IOPV',
            field: 'punblish',
            minWidth: 150,
            sortable: true,
            align: 'center',
            filters: objToVxeFilterArr(this.yesOrNoPojo),
            render: value => this.yesOrNoPojo[value],
        },
        {
            title: '当天状态',
            field: 'creationredemType',
            minWidth: 120,
            sortable: true,
            align: 'center',
            filters: objToVxeFilterArr(this.creationredemTypePojo),
            render: value => this.creationredemTypePojo[value],
        },
        { title: '成分股数量', field: 'recordNum', minWidth: 120, sortable: true, align: 'right', render: value => this.formatterNumber(value) },
        {
            title: 'T日预估现金差额',
            field: 'estimatecash',
            minWidth: 200,
            sortable: true,
            align: 'right',
            render: value => this.formatterNumber(value),
        },
        { title: 'T-1日现金余额', field: 'cash', minWidth: 180, sortable: true, align: 'right', render: value => this.formatterNumber(value) },

        // {
        //     title: 'T日申购预估现金差额',
        //     field: 'estimatecash',
        //     minWidth: 200,
        //     sortable: true,
        //     align: 'right',
        //     render: value => this.formatterNumber(value),
        // },

        { title: 'T日日期', field: 'tradeDate', minWidth: 120, sortable: true, align: 'center', render: value => dateFormat(value) || '--' },
        { title: 'T-1日', field: 'preTradeDate', minWidth: 120, sortable: true, align: 'center', render: value => dateFormat(value) || '--' },
        // { title: 'T-1日申购现金余额', field: 'cash', minWidth: 180, sortable: true, align: 'right', render: value => this.formatterNumber(value) },
        {
            title: 'T-1日基准单位净值',
            field: 'navPercu',
            minWidth: 180,
            sortable: true,
            align: 'right',
            render: value => this.formatterNumber(value),
        },
        { title: 'T-1日单位净值', field: 'nav', minWidth: 150, sortable: true, align: 'right', render: value => this.formatterNumber(value) },
        // { title: '文件路径', field: 'path', minWidth: 120, sortable: true },
        { title: '文件日期', field: 'fileDate', minWidth: 120, sortable: true, align: 'center', render: value => dateFormat(value) || '--' },

        { title: '文件时间', field: 'fileTime', minWidth: 120, sortable: true, align: 'center' },

        {
            title: '接口类型',
            field: 'importType',
            minWidth: 120,
            sortable: true,
            filters: objToVxeFilterArr(this.importTypePojo),
            align: 'center',

            render: value => this.importTypePojo[value] || value,
        },
        {
            title: 'ETF市场类型',
            field: 'type',
            minWidth: 160,
            sortable: true,
            filters: objToVxeFilterArr(this.ETFMarketTypePojo),
            align: 'center',
            render: value => this.ETFMarketTypePojo[value] || value,
        },
        {
            title: 'ETF分类',
            field: 'etfType',
            minWidth: 120,
            sortable: true,
            filters: objToVxeFilterArr(this.ETFTypePojo),
            align: 'center',
            render: value => this.ETFTypePojo[value] || value,
        },
        {
            title: '当日申购可赎回',
            field: 'todayCanRedeem',
            minWidth: 160,
            sortable: true,
            align: 'center',
            filters: objToVxeFilterArr(this.yesOrNoPojo),
            render: value => this.yesOrNoPojo[value],
        },
        {
            title: '对方市场',
            field: 'rivalMarket',
            minWidth: 120,
            sortable: true,
            filters: objToVxeFilterArr(this.marketPojo),
            align: 'center',
            render: value => this.marketPojo[value] || value,
        },
        { title: '资产处理日', field: 'assetDealDate', minWidth: 120, sortable: true, align: 'right', render: value => dateFormat(value) || '--' },
        { title: '冲击成本率', field: 'impactCostRate', minWidth: 120, sortable: true, align: 'right' },

        // {
        //     title: 'T日赎回预估现金余额',
        //     field: 'estimatecash',
        //     minWidth: 180,
        //     sortable: true,
        //     align: 'right',
        //     render: value => this.formatterNumber(value),
        // },
        // { title: 'T-1日赎回现金余额', field: 'cash', minWidth: 180, sortable: true, align: 'right', render: value => this.formatterNumber(value) },
    ];

    /**
     * PCF基础数据 —— 成分股信息表格字段
     */
    stockColumns: ColumnVO[] = [
        // { field: 'serial', title: '序号', sortable: true, minWidth: 100, filterType: 'search', filters: [{ data: '' }] },
        {
            field: 'marketCode',
            title: '交易所',
            sortable: true,
            minWidth: 140,
            filters: objToVxeFilterArr(this.marketPojo),
            align: 'center',
            render: value => this.marketPojo[value] || '--',
        },
        { field: 'stockCode', title: '证券代码', sortable: true, minWidth: 140, filterType: 'search', filters: [{}] },
        { field: 'stockName', title: '证券名称', sortable: true, minWidth: 140, filterType: 'search', filters: [{}] },
        { field: 'stockAmount', title: '证券数量', sortable: true, minWidth: 140, align: 'right', render: value => this.formatterNumber(value) },
        {
            field: 'purchaseBalance',
            title: '申购替代金额',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: value => this.formatterNumber(value),
        },
        {
            field: 'redeemBalance',
            title: '赎回替代金额',
            sortable: true,
            minWidth: 140,
            align: 'right',
            render: value => this.formatterNumber(value),
        },
        {
            field: 'cashType',
            title: '替代标志',
            sortable: true,
            minWidth: 140,
            align: 'center',
            filters: objToVxeFilterArr(this.cashTypePojo),
            render: value => this.cashTypePojo[value] || value,
        },
        { field: 'priceRatio', title: '溢价比率', sortable: true, minWidth: 140, align: 'right', render: value => this.formatterNumber(value) },
    ];

    /**
     * 成分股信息排序字段
     */
    etfStockSortFields: any = {
        marketCode: 'C_MARKET_CODE',
        stockCode: 'C_STOCK_CODE',
        stockName: 'C_STOCK_NAME',
        stockAmount: 'N_STOCK_AMOUNT',
        purchaseBalance: 'N_PURCHASE_BALANCE',
        redeemBalance: 'N_REDEEM_BALANCE',
        cashType: 'C_CASH_TYPE',
        priceRatio: 'N_PRICE_RATIO',
    };

    /** ******************************************* PCF基础数据end ******************************************** */

    /** ***************************************** 风险试算导出 ********************************** */

    /**
     * ETF成分股导出
     */
    importETFStockColumns = [
        { title: 'ETF指令关联编号', dataIndex: 'associationNo' },
        { title: '产品代码/简称', dataIndex: 'fundCode' },
        { title: '资产单元编号/名称', dataIndex: 'assetCode' },
        { title: '组合编码/名称', dataIndex: 'fundAccountCode' },
        { title: '申报代码/名称', dataIndex: 'stockCode' },
        // { title: '序号', dataIndex: 'no' },
        { title: '交易所', dataIndex: 'marketCode' },
        { title: '证券代码', dataIndex: 'stockCode' },
        { title: '证券名称', dataIndex: 'stockName' },
        { title: '持仓数量', dataIndex: 'positionAmount' },
        { title: '可用数量', dataIndex: 'availableAmount' },
        { title: '单位数量', dataIndex: 'stockAmount' },
        { title: '价格', dataIndex: 'price' },
        { title: '市值', dataIndex: 'marketValue' },
        { title: '计算申购/赎回数量', dataIndex: 'calApplyAmount' },
        { title: '实际申购/赎回数量', dataIndex: 'actualApplyAmount' },
        { title: '溢价/折价比率(%)', dataIndex: 'priceRatio' },
        { title: '替代标志', dataIndex: 'cashType' },
        { title: '替代数量', dataIndex: 'alternativeAmount' },
        { title: '替代金额', dataIndex: 'purchaseBalance' },
    ];
}
